﻿using Newtonsoft.Json;
using System;
using System.Web.Http;
using Ymatou.API.Common;
using Ymatou.CommonService;
using Ymatou.Evaluate.Buyer.Model.ApiModel.Request.Query;
using Ymatou.Evaluate.Buyer.Model.ApiModel.Response.Query;
using Ymatou.PerfMonitorClient;

namespace BuyerEvaluateQuery.Api.Controllers
{
    public class EvaluateQueryController : BaseController
    {
        /// <summary>
        /// 获取订单评价状态
        /// </summary>
        /// <param name="request"></param>
        /// <returns></returns>
        [Route("api/GetOrdersEvalStatus")]
        [HttpPost]
        public BaseResponseModel<GetOrdersEvalStatusResponse> GetOrdersEvalStatus([FromBody]GetOrdersEvalStatusRequest request)
        {
            using (MethodMonitor.New("api/GetOrdersEvalStatus"))
            {
                try
                {
                    if (request == null) throw new BussniessException("-1", "请求参数不能传入null");
                    request.Validate();
                    var response = EvaluateQueryService.GetOrdersEvalStatus(request);
                    return new SuccessResponseModel<GetOrdersEvalStatusResponse>(response);
                }
                catch (BussniessException ex)
                {
                    ApplicationLog.Info(string.Format("api/GetOrdersEvalStatus业务异常：Code={0},Msg={1}", ex.ErrorCode, ex.ErrorMsg));
                    return new BussniessErrorResponseModel<GetOrdersEvalStatusResponse>(int.Parse(ex.ErrorCode), ex.ErrorMsg);
                }
                catch (Exception ex)
                {
                    ApplicationLog.Error("api/GetOrdersEvalStatus异常", ex);
                    ApplicationLog.Info(string.Format("api/GetOrdersEvalStatus异常请求参数:{0}",
                        JsonConvert.SerializeObject(request)));
                    return new SystemErrorResponseModel<GetOrdersEvalStatusResponse>(ex.Message);
                }
            }
        }

        /// <summary>
        /// 获取商品评价列表
        /// </summary>
        /// <param name="request"></param>
        /// <returns></returns>
        [Route("api/GetProdEvaluateList")]
        [HttpGet]
        public BaseResponseModel<GetProdEvaluateListResponse> GetProdEvaluateList([FromUri]GetProdEvaluateListRequest request)
        {
            using (MethodMonitor.New("api/GetProdEvaluateList"))
            {
                try
                {
                    if (request == null) throw new BussniessException("-1", "请求参数不能传入null");
                    request.Validate();
                    var response = EvaluateQueryService.GetProdEvaluateList(request);
                    return new SuccessResponseModel<GetProdEvaluateListResponse>(response);
                }
                catch (BussniessException ex)
                {
                    ApplicationLog.Info(string.Format("api/GetProdEvaluateList业务异常：Code={0},Msg={1}", ex.ErrorCode, ex.ErrorMsg));
                    return new BussniessErrorResponseModel<GetProdEvaluateListResponse>(int.Parse(ex.ErrorCode), ex.ErrorMsg);
                }
                catch (Exception ex)
                {
                    ApplicationLog.Error("api/GetProdEvaluateList异常", ex);
                    ApplicationLog.Info(string.Format("api/GetProdEvaluateList异常请求参数:{0}",
                        JsonConvert.SerializeObject(request)));
                    return new SystemErrorResponseModel<GetProdEvaluateListResponse>(ex.Message);
                }
            }
        }

        /// <summary>
        /// 获取商品评价汇总
        /// </summary>
        /// <param name="request"></param>
        /// <returns></returns>
        [Route("api/GetProductSummary")]
        [HttpGet]
        public BaseResponseModel<GetProductSummaryResponse> GetProductSummary([FromUri]GetProductSummaryRequest request)
        {
            using (MethodMonitor.New("api/GetProductSummary"))
            {
                try
                {
                    if (request == null) throw new BussniessException("-1", "请求参数不能传入null");
                    request.Validate();
                    var response = EvaluateQueryService.GetProductSummary(request);
                    return new SuccessResponseModel<GetProductSummaryResponse>(response);
                }
                catch (BussniessException ex)
                {
                    ApplicationLog.Info(string.Format("api/GetProductSummary业务异常：Code={0},Msg={1}", ex.ErrorCode, ex.ErrorMsg));
                    return new BussniessErrorResponseModel<GetProductSummaryResponse>(int.Parse(ex.ErrorCode), ex.ErrorMsg);
                }
                catch (Exception ex)
                {
                    ApplicationLog.Error("api/GetProductSummary异常", ex);
                    ApplicationLog.Info(string.Format("api/GetProductSummary异常请求参数:{0}",
                        JsonConvert.SerializeObject(request)));
                    return new SystemErrorResponseModel<GetProductSummaryResponse>(ex.Message);
                }
            }
        }

        /// <summary>
        /// 获取买家待评价订单Id列表
        /// </summary>
        /// <param name="request"></param>
        /// <returns></returns>
        [Route("api/GetPendingEvaluateOrders")]
        [HttpGet]
        public BaseResponseModel<GetPendingEvaluateOrdersResponse> GetPendingEvaluateOrders([FromUri]GetPendingEvaluateOrdersRequest request)
        {
            using (MethodMonitor.New("api/GetPendingEvaluateOrders"))
            {
                try
                {
                    if (request == null) throw new BussniessException("-1", "请求参数不能传入null");
                    request.Validate();
                    var response = EvaluateQueryService.GetPendingEvaluateOrders(request);
                    return new SuccessResponseModel<GetPendingEvaluateOrdersResponse>(response);
                }
                catch (BussniessException ex)
                {
                    ApplicationLog.Info(string.Format("api/GetPendingEvaluateOrders业务异常：Code={0},Msg={1}", ex.ErrorCode, ex.ErrorMsg));
                    return new BussniessErrorResponseModel<GetPendingEvaluateOrdersResponse>(int.Parse(ex.ErrorCode), ex.ErrorMsg);
                }
                catch (Exception ex)
                {
                    ApplicationLog.Error("api/GetPendingEvaluateOrders异常", ex);
                    ApplicationLog.Info(string.Format("api/GetPendingEvaluateOrders异常请求参数:{0}",
                        JsonConvert.SerializeObject(request)));
                    return new SystemErrorResponseModel<GetPendingEvaluateOrdersResponse>(ex.Message);
                }
            }
        }

        /// <summary>
        /// 获取买家待评价订单总数
        /// </summary>
        /// <param name="request"></param>
        /// <returns></returns>
        [Route("api/GetPendingEvaluateCount")]
        [HttpGet]
        public BaseResponseModel<GetPendingEvaluateCountResponse> GetPendingEvaluateCount([FromUri]GetPendingEvaluateCountRequest request)
        {
            using (MethodMonitor.New("api/GetPendingEvaluateCount"))
            {
                try
                {
                    if (request == null) throw new BussniessException("-1", "请求参数不能传入null");
                    request.Validate();
                    var response = EvaluateQueryService.GetPendingEvaluateCount(request);
                    return new SuccessResponseModel<GetPendingEvaluateCountResponse>(response);
                }
                catch (BussniessException ex)
                {
                    ApplicationLog.Info(string.Format("api/GetPendingEvaluateCount业务异常：Code={0},Msg={1}", ex.ErrorCode, ex.ErrorMsg));
                    return new BussniessErrorResponseModel<GetPendingEvaluateCountResponse>(int.Parse(ex.ErrorCode), ex.ErrorMsg);
                }
                catch (Exception ex)
                {
                    ApplicationLog.Error("api/GetPendingEvaluateCount异常", ex);
                    ApplicationLog.Info(string.Format("api/GetPendingEvaluateCount异常请求参数:{0}",
                        JsonConvert.SerializeObject(request)));
                    return new SystemErrorResponseModel<GetPendingEvaluateCountResponse>(ex.Message);
                }
            }
        }
    }
}
